package com.qinggeng.project.system.service.impl;

import com.qinggeng.common.enums.CheckStatusEnum;
import com.qinggeng.common.enums.CommonEnum;
import com.qinggeng.common.enums.ShowTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.project.api.dto.PositionDto;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.system.domain.Company;
import com.qinggeng.project.system.domain.Position;
import com.qinggeng.project.system.domain.PositionClassify;
import com.qinggeng.project.system.domain.PositionName;
import com.qinggeng.project.system.mapper.PositionMapper;
import com.qinggeng.project.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import net.coobird.thumbnailator.geometry.Positions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PositionServiceImpl implements IPositionService {

    @Resource
    private PositionMapper positionMapper;
    @Autowired
    private IPositionClassifyService positionClassifyService;
    @Autowired
    private IPositionNameService positionNameService;
    @Autowired
    private IJobUserService jobUserService;
    @Autowired
    private ICompanyService companyService;

    @Override
    public boolean insertPosition(Position position) {
        position.setName(positionNameService.checkPositionName(position.getPositionCode()).getName());
        position.setStatus(CheckStatusEnum.审核中.getValue());//10=审核中
        return positionMapper.insertPosition(position)>0;
    }

    @Override
    public boolean deletePositionById(Long id) {
        return positionMapper.deletePositionById(id)>0;
    }

    @Override
    public int deletePositionByIds(Long[] ids) {
        return positionMapper.deletePositionByIds(ids);
    }

    @Override
    public boolean updatePosition(Position position) {
        position.setName(positionNameService.checkPositionName(position.getPositionCode()).getName());
        position.setStatus(CheckStatusEnum.审核中.getValue());
        return positionMapper.updatePosition(position)>0;
    }

    @Override
    public boolean checkPosition(Position position) {
        return positionMapper.updatePosition(position)>0;
    }

    @Override
    public List<PositionDto> listPositionDto(Position where) {
        where.setStatus(CheckStatusEnum.已通过.getValue());
        return positionMapper.listPositionDto(where);
    }

    public List<PositionDto> listCollectPositionDto(){
        Long jobUserId=jobUserService.getJobUserId();
        return positionMapper.listCollectPositionDtoByJobUserId(jobUserId);
    }

    public List<PositionDto> listBrowsePositionDto(){
        Long jobUserId=jobUserService.getJobUserId();
        return positionMapper.listBrowsePositionDtoByJobUserId(jobUserId);
    }


    @Override
    public List<Position> listPosition(Position where) {
        return positionMapper.listPosition(where);
    }

    @Override
    public PositionDto getPositionDtoById(Long id) {
        PositionDto positionDto=positionMapper.getPositionDtoById(id);
        if(null==positionDto){
            throw new CustomException("岗位不存在或已被删除",ShowTypeEnum.用户.getValue());
        }
        positionMapper.updatePositionBrowseCount(id);
        return positionDto;
    }

    @Override
    public Position getPositionById(Long id) {
        return positionMapper.getPositionById(id);
    }

    @Override
    public Integer getPositionBrowseCountById(Long id){
        Position position=this.getPositionById(id);
        if(null==position||null==position.getBrowseCount()){
            return 0;
        }
        return position.getBrowseCount();
    }

    @Override
    public Integer getPositionCollectCountByUserId(){
        List<PositionDto> positionDtoList=this.listCollectPositionDto();
        if(CollectionUtils.isEmpty(positionDtoList)){
            return 0;
        }
        return positionDtoList.size();
    }

    @Override
    public Position getPositionByIdAndJobUserId(Long id) {
        Long jobUserId=jobUserService.getJobUserId();
        Position position=positionMapper.getPositionByIdAndJobUserId(id,jobUserId);
        if(null==position){
           throw new CustomException("职位不存在或没操作权限！",ShowTypeEnum.用户.getValue());
        }
        return position;
    }

    @Override
    public String getPositionPhoneNumber(Long id) {
        String contact=positionMapper.getPositionPhoneNumber(id);
        if(StringUtils.isEmpty(contact)){
            throw new CustomException("您要查看的联系方式不存在！", ShowTypeEnum.用户.getValue());
        }
        return contact;
    }


}
